package admin

import (
	"fmt"
	"github.com/astaxie/beego"
	"html/template"
	"strconv"
	"xianzhi/service"
	"xianzhi/utils"
)

type UserAdminRoleController struct {
	BaseController
}

func (c *UserAdminRoleController) Add() {
	p := map[string]interface{}{}
	c.Data["authorityList"] = service.GetAllAuthority(p)

	p = map[string]interface{}{}
	p["status"] = 1
	_, c.Data["schoolList"] = service.GetSchoolList(p)

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/user_admin_role/add.html"
}

func (c *UserAdminRoleController) DoAdd() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if len(input["name"][0]) <= 0 {
		res = utils.ResJson(0, "请输入角色名称", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	userAdminRoleId, err := service.AddUserAdminRole(input)
	if err != nil {
		res = utils.ResJson(0, "添加失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	input["user_admin_role_id"] = []string{fmt.Sprintf("%d", userAdminRoleId)}
	_, err = service.AddUserAdminRoleAuthority(input)
	if err != nil {
		res = utils.ResJson(0, "添加失败" + err.Error(), "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "添加成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *UserAdminRoleController) Edit() {
	id := utils.Atoi64(c.Ctx.Input.Param(":id"))

	p := map[string]interface{}{}
	p["status"] = 1
	_, c.Data["schoolList"] = service.GetSchoolList(p)

	p = map[string]interface{}{}
	p["userAdminRoleId"] = id
	c.Data["authorityList"] = service.GetAllAuthority(p)

	p = map[string]interface{}{}
	p["id"] = id
	c.Data["record"] = service.GetUserAdminRoleInfo(p)

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/user_admin_role/edit.html"
}

func (c *UserAdminRoleController) DoEdit() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if len(input["name"][0]) <= 0 {
		res = utils.ResJson(0, "请输入角色名称", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	_, err := service.EditUserAdminRole(input)
	if err != nil {
		res = utils.ResJson(0, "修改失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	_, err = service.EditUserAdminRoleAuthority(input)
	if err != nil {
		res = utils.ResJson(0, "修改失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "修改成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *UserAdminRoleController) ShowList() {
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}
	perCount, _ := beego.AppConfig.Int("percount")
	symPageCount, _ := beego.AppConfig.Int("symmetricpagecount")
	p := map[string]interface{}{}
	p["excludeSuper"] = true
	p["relation"] = true
	p["curPage"] = curPage
	p["perCount"] = perCount
	totalCount, recordList := service.GetUserAdminRoleList(p)
	paginator := utils.NewPaginator(int(totalCount), perCount, symPageCount, curPage)
	c.Data["paginator"] = paginator.GetPageHtml()
	c.Data["recordList"] = recordList
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/user_admin_role/list.html"
}